package leetcode.n2021;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode-cn.com/problems/keyboard-row/description/
 * https://leetcode.com/problems/keyboard-row/?tab=Description
 * @program: brushing
 * @description:
 * @author: LiuQiang
 * @create: 2018-07-25 10:35
 **/
public class KeyboardRow {
    public String[] findWords(String[] words) {
        List<String> aList = new ArrayList<String>();
        aList.add("q");
        aList.add("w");
        aList.add("e");
        aList.add("r");
        aList.add("t");
        aList.add("y");
        aList.add("u");
        aList.add("i");
        aList.add("o");
        aList.add("p");
        List<String> bList = new ArrayList<String>();
        bList.add("a");
        bList.add("s");
        bList.add("d");
        bList.add("f");
        bList.add("g");
        bList.add("h");
        bList.add("j");
        bList.add("k");
        bList.add("l");
        List<String> cList = new ArrayList<String>();
        cList.add("z");
        cList.add("x");
        cList.add("c");
        cList.add("v");
        cList.add("b");
        cList.add("n");
        cList.add("m");

        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            String s[] = word.split("");
            boolean flag = true;

            if (aList.contains(s[0].toLowerCase())) {
                for (int j = 0; j < s.length; j++) {
                    if (!aList.contains(s[j].toLowerCase())) {
                        flag = false;
                    }
                }
            }
            if (bList.contains(s[0].toLowerCase())) {
                for (int j = 0; j < s.length; j++) {
                    if (!bList.contains(s[j].toLowerCase())) {
                        flag = false;
                    }
                }
            }
            if (cList.contains(s[0].toLowerCase())) {
                for (int j = 0; j < s.length; j++) {
                    if (!cList.contains(s[j].toLowerCase())) {
                        flag = false;
                    }
                }
            }

            if (flag) {
                sb.append(words[i].toString() + ",");
            }
        }

        if (sb == null || sb.length() == 0) {
            String a[] = {};
            return a;
        } else {
            String a[] = sb.toString().split(",");
            return a;
        }
    }

    public static void main(String[] args) {
        String a[] = {"Hello", "Alaska", "Dad", "Peace"};
        String b[] = new KeyboardRow().findWords(a);
        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);
        }
    }
}
